﻿Imports Control_Vuelos_BE
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class M_EmpresaTransporteADO
    Dim MiConexion As New ConexionADO
    Dim cmd As New SqlCommand

    Public Function ListarM_EmpresaTransporte(ByVal p_CodEmpresa As String) As DataTable
        Try
            Dim cn As New SqlConnection(MiConexion.GetCnx)
            Using cn
                Dim cmd As New SqlCommand("sp_Lista_M_EmpresaTransporte", cn)
                Using cmd
                    cmd.CommandType = Data.CommandType.StoredProcedure
                    cmd.Parameters.Add("@p_CodEmpresa", SqlDbType.Char, 3)
                    cmd.Parameters("@p_CodEmpresa").Value = IIf(p_CodEmpresa Is String.Empty, Nothing, p_CodEmpresa.Trim)
                    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable("Empresa")
                    da.Fill(dt)
                    Return dt
                End Using
            End Using
        Catch myException As Exception
            MsgBox("M_EmpresaTransporteADO (ListarM_EmpresaTranporte) --> " & myException.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function

    Public Function pr_AgregarM_EmpresaTransporte(ByVal p_M_EmpresaTransporteBE As M_EmpresaTransporteBE) As Boolean
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            cn.Open()
            Dim oTran As SqlTransaction = cn.BeginTransaction(IsolationLevel.Serializable)
            Dim sInstruccion As String = "INSERT INTO dbo.M_EmpresaTransporte(CodEmpresa,NomEmpresa,CodAnalisis) VALUES ( " & _
                "@CodEmpresa,@NomEmpresa,@CodAnalisis) "
            Dim cmd As New SqlCommand(sInstruccion, cn)
            cmd.Transaction = oTran
            cmd.Parameters.AddWithValue("@CodEmpresa", p_M_EmpresaTransporteBE.CodEmpresa)
            cmd.Parameters.AddWithValue("@NomEmpresa", p_M_EmpresaTransporteBE.NomEmpresa)
            cmd.Parameters.AddWithValue("@CodAnalisis", p_M_EmpresaTransporteBE.CodAnalisis)
            Try
                cmd.ExecuteNonQuery()
                oTran.Commit()
                Return True
            Catch myException As Exception
                MsgBox("M_EmpresaTransporteADO (pr_AgregarM_EmpresaTransporte) --> " & myException.Message, MsgBoxStyle.Critical)
                oTran.Rollback()
                Return False
            End Try
        End Using
    End Function

    Public Function pr_ModificarM_EmpresaTransporte(ByVal p_M_EmpresaTransporteBE As M_EmpresaTransporteBE) As Boolean
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            cn.Open()
            Dim oTran As SqlTransaction = cn.BeginTransaction(IsolationLevel.Serializable)
            Dim sInstruccion As String = "UPDATE dbo.M_EmpresaTransporte  SET NomEmpresa= @NomEmpresa, CodAnalisis =@CodAnalisis WHERE " & _
                " CodEmpresa  = @CodEmpresa "
            Dim cmd As New SqlCommand(sInstruccion, cn)
            cmd.Transaction = oTran
            cmd.Parameters.AddWithValue("@CodEmpresa", p_M_EmpresaTransporteBE.CodEmpresa)
            cmd.Parameters.AddWithValue("@NomEmpresa", p_M_EmpresaTransporteBE.NomEmpresa)
            cmd.Parameters.AddWithValue("@CodAnalisis", p_M_EmpresaTransporteBE.CodAnalisis)
            Try
                cmd.ExecuteNonQuery()
                oTran.Commit()
                Return True
            Catch myException As Exception
                MsgBox("M_EmpresaTransporteADO (pr_ModificarM_EmpresaTransporte) --> " & myException.Message, MsgBoxStyle.Critical)
                oTran.Rollback()
                Return False
            End Try
        End Using
    End Function


    Public Function pr_EliminarM_EmpresaTransporte(ByVal p_M_EmpresaTransporteBE As M_EmpresaTransporteBE) As Boolean
        Dim cn As New SqlConnection(MiConexion.GetCnx)
        Using cn
            cn.Open()
            Dim oTran As SqlTransaction = cn.BeginTransaction(IsolationLevel.Serializable)
            Dim sInstruccion As String = "DELETE FROM dbo.M_EmpresaTransporte  WHERE " & _
                " CodEmpresa  = @CodEmpresa "
            Dim cmd As New SqlCommand(sInstruccion, cn)
            cmd.Transaction = oTran
            cmd.Parameters.AddWithValue("@CodEmpresa", p_M_EmpresaTransporteBE.CodEmpresa)
            Try
                cmd.ExecuteNonQuery()
                oTran.Commit()
                Return True
            Catch myException As Exception
                MsgBox("M_EmpresaTransporteADO (pr_EliminarM_EmpresaTransporte) --> " & myException.Message, MsgBoxStyle.Critical)
                oTran.Rollback()
                Return False
            End Try
        End Using
    End Function

End Class
